﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Model.Entities;
using CSP.Model.Model;

namespace CSP.Model.Presenter
{
    public class MyUpdatesPresenter
    {
        private IMyUpdatesPresenter view = null;
        private ModelManager model = new ModelManager();

        public MyUpdatesPresenter(IMyUpdatesPresenter iView)
        {
            view = iView;
        }

        public void Load()
        {
            try
            {
                List<UserChange> changes = model.GetMyUpdates();
                List<Update> updates = new List<Update>();

                foreach (UserChange uc in changes)
                {
                    updates.Add(FormatUpdate(uc));
                }

                view.BindMyUpdates(updates);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private Update FormatUpdate(UserChange uc)
        {
            Update update = new Update();

            try
            {
                switch (uc.UserChangeType)
                {
                    case UserChange.UserChangeTypes.AnniversaryChange:
                        update.UpdateEvent = string.Format("{0}'s anniversary has changed to: {1}", uc.UserNameChange, uc.anniversaryChange);
                        update.UpdateIcon = "anniversary";
                        break;
                    case UserChange.UserChangeTypes.BlogChange:
                        update.UpdateEvent = string.Format("{0} posted a new blog: <b>'{1}'</b>", uc.UserNameChange, uc.blogTitleChange);
                        update.UpdateIcon = "blog";
                        break;
                    case UserChange.UserChangeTypes.DocumentChange:
                        update.UpdateEvent = string.Format("{0} posted a new document: <b>'{1}'</b>", uc.UserNameChange, uc.blogTitleChange);
                        update.UpdateIcon = "blog";
                        break;
                    case UserChange.UserChangeTypes.ColleagueChange:
                        if (uc.ColleagueChangeType == UserChange.ColleagueChangeTypes.Add)
                        {
                            update.UpdateEvent = string.Format("{0} and <b>{1}</b> are now friends", uc.UserNameChange, uc.colleagueNameChange);
                        }
                        else
                        {
                            update.UpdateEvent = string.Format("{0} removed {1} from Colleagues list", uc.UserNameChange, uc.colleagueNameChange);
                        }
                        update.UpdateIcon = "colleague";
                        break;
                    case UserChange.UserChangeTypes.SiteMembershipChange:
                        update.UpdateEvent = string.Format("{0} has joined the membership: <b>{1}</b>", uc.UserNameChange, uc.membershipChange);
                        update.UpdateIcon = "membership";
                        break;
                    case UserChange.UserChangeTypes.StatusChange:
                        update.UpdateEvent = string.Format("Status update: {0}", uc.statusChange);
                        update.UpdateIcon = "status";
                        break;
                    case UserChange.UserChangeTypes.Tweet:
                        update.UpdateEvent = string.Format("{0}", uc.blogTitleChange);
                        update.UpdateIcon = "blog";
                        break;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return update;
        }
    }
}
